package jgl.vajra.management.widget.behavior;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import jgl.vajra.management.R;
import jgl.vajra.management.widget.utils.DisplayUtils;

import com.google.android.material.appbar.AppBarLayout;

import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.content.ContextCompat;
import androidx.vectordrawable.graphics.drawable.ArgbEvaluator;

public class ColorGradBehavior extends CoordinatorLayout.Behavior<View> {
    private static final String TAG = "ColorGradBehavior";
    private int PAGE_COLOR_ONE;
    private int PAGE_COLOR_TWO;

    public ColorGradBehavior() {
    }

    public ColorGradBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
        PAGE_COLOR_ONE = ContextCompat.getColor(context, R.color.transparent);
        PAGE_COLOR_TWO = ContextCompat.getColor(context, R.color.colorAccent);
    }

    //所依赖的联动对象
    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
        return dependency instanceof AppBarLayout;
    }

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
        float dy = dependency.getHeight() + dependency.getY();
        float y = dy / child.getHeight()+ DisplayUtils.getDimension(R.dimen.dp_50);
        y = y > 1 ? 1 : y;
        ArgbEvaluator argbEvaluator = new ArgbEvaluator();//渐变色计算类
        int currentLastColor = (int) (argbEvaluator.evaluate(1 - y, PAGE_COLOR_ONE, PAGE_COLOR_TWO));
        child.setBackgroundColor(currentLastColor);
        return true;
    }
}
